package com.itheima.tlias84.controller;

import com.itheima.tlias84.Po.InsertDeptParam;
import com.itheima.tlias84.Service.DeptService;
import com.itheima.tlias84.Util.ResultUtil;
import com.itheima.tlias84.dto.SelectAllDeptDTO;
import com.itheima.tlias84.entity.Dept;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/depts")
/**
 * @RequestMapping  Target ->类上,方法上
 *                  类上 ->提取一个controller中的重复路径
 *                  方法上 -> 定义该接口的请求方法的 被对应的GetMapping ..PostMapping等替代了
 */
public class DeptController {
//属性注入优先使用@Resource
//    @Autowired
//    DeptService deptService;
    @Resource
    DeptService service;

    //找不到depts的话就会报404错误
    @GetMapping
//    @RequestMapping(method= RequestMethod.GET)

//    public Map<String ,Object> selectEmpByPage(){
    /**
     * 查询所有部门信息
     */
    public ResultUtil selectAll(){
     //   DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        List<SelectAllDeptDTO> depts = service.selectAll();
  //      List<SelectAllDeptDTO> list = deptMapper.selectEmpByPage();
//        depts.stream().map(dept -> {
//            SelectAllDeptDTO dto = new SelectAllDeptDTO();
//            dto.setId(dept.getId());
//            dto.setName(dept.getName());
//            String format = dept.getUpdateTime().format(pattern);
//            dto.setUpdateTime(format);
//            return dto;
//        }).toList();
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("code",1);
//        map.put("msg","查询成功");
//        map.put("data",depts);
//        return map;
        return ResultUtil.success(depts);
    }

    /**
     * get/delete 请求 参数一般拼接到url后面
     * post/put 请求参数一般放在请求体里面,且以json格式传输
     * @RequestBody 作用就是从http请求体中读传输的数据
     *              作用2 把json字符串转换为对象
     * @param
     * @return
     */
    @PostMapping
//    @RequestMapping(method= RequestMethod.POST)

   // public Map<String,Object> insertDept(@RequestBody InsertDeptParam param) {
    public ResultUtil  insertDept(@RequestBody InsertDeptParam param) {
        //把JSON字符串转换为对象
        log.info("读取请求体中的数据{}",param.getName());
        int i = service.insertDept(param);
        return i==0?ResultUtil.fail("新增失败"):ResultUtil.success("新增成功");
//        Dept dept = new Dept();
//        dept.setName(param.getName());
//        dept.setUpdateTime(LocalDateTime.now());
//        dept.setCreateTime(LocalDateTime.now());
//        int flag = loginservice.insertDept(dept);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("code",flag);
//        map.put("msg","查询成功");
//        map.put("data",null);
//        return ResultUtil.success();



//   public  String insertDept( HttpServletRequest request) {
//        try {
//            BufferedReader reader = request.getReader();
//            log.info("读取请求体中的数据{}",reader.readLine());
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

    }

    /**
     * @RequestParam 注解的作用:
     *              1.根据指定的k,获取http url的值 valuue/name属性
     *              2.可以根据是否传参数, 如果指定必传,前端没传相应参数->400 required
     *              3.给参数指定数据默认值 defaultValue 默认值属性
     * @param id
     * @return
     */
    @DeleteMapping
    public ResultUtil deleteById(Integer id) {
        log.info("删除的id{}",id);
        int i = service.deleteById(id);
        return i==0?ResultUtil.fail("删除失败"):ResultUtil.success("删除成功");
    }

    /**
     * @PathVariable 注解作用
     *          获取URI以/分割的路径变量
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultUtil selectDeptById(@PathVariable Integer id ) {
     log.info("使用PathVariable 注解获取{}",id);
        SelectAllDeptDTO dto = service.selectDeptById(id);
        return ResultUtil.success(dto);
//        String requestURI = request.getRequestURI();
//        log.info("路径参数URI={}",requestURI);
//        String[] split = requestURI.split("/");
//        log.info("路径参数id={}",split[split.length-1]);

    }

    @PutMapping
    public ResultUtil updateDeptById(@RequestBody Dept dept) {
       log.info("修改的部门信息为{}",dept.getName());
//        int flag = loginservice.updateDeptById(dept);
//       return flag==0?ResultUtil.fail():ResultUtil.success();
        return service.updateDeptById(dept);
    }

    @DeleteMapping("batchDelete")
   // public ResultUtil batchDelete(@RequestParam("ids") int[]ids) {
//    public ResultUtil batchDelete(@RequestParam("ids") List<Integer> ids) {
    public ResultUtil batchDelete(@RequestParam("ids") List<Integer> ids, HttpServletRequest request) {
        String[]ids1 = request.getParameterValues("ids");
        log.info("通过servlet取重参数",ids1);
        log.info("批量删除的id{}",ids);
         service.batchDelete(ids);
        return ResultUtil.success("删除成功");
    }

    @PostMapping("batchInsert")
    public ResultUtil batchInsert(@RequestParam("namelist") List<String> namelist) {
        log.info("批量插入的部门信息{}",namelist);
        service.batchInsert(namelist);
        return ResultUtil.success("批量插入成功");
    }




 }


